﻿namespace GreenIsomer.Web.Controls.JQuery {
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Text;

	/// <summary>
	/// Uses System.Web.UI.Control to provide a standard base implementation of the IJQueryControl contract requried by the Manager control to generate JQuery/JavaScript in addition to HTML markup typical of a control.
	/// </summary>
	public abstract class JQueryControl : Control, IJQueryControl {


		#region Constants: Property defaults

		private const bool defaultUseCssSyling = false;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the GreenIsomer.Web.Controls.JQueryControl base class.
		/// </summary>
		protected JQueryControl() { }

		#endregion


		#region Properties

		/// <summary>
		/// Gets or sets a value instructing the server control not to render style defined as HTML attributes on the element, rather requiring the site's stylesheet to handle functionality required to properly render the client UI.
		/// This value can be overridden at the GreenIsomer.Web.Controls.JQuery.Manager level rather than setting each individual control.
		/// </summary>
		public bool UseCssStyling {
			get { return ViewState.GetValue("UseCssStyling", defaultUseCssSyling); }
			set { ViewState["UseCssStyling"] = value; }
		}

		/// <summary>
		/// Gets a value using the jQuery Manager as well as control itself that instructs the control not to render style as HTML attributes on the element, forcing the site developer to include the values in the site's stylesheet.
		/// This method is used primarily by control developers.
		/// </summary>
		protected bool MasterUseCssStyling {
			get {
				switch(Manager.GetCurrent(Page).UseCssStyling) {
					case MasterBoolean.ByControl: return UseCssStyling;
					case MasterBoolean.Always: return true;
					case MasterBoolean.Never: return false;
					default: return UseCssStyling;
				}
			}
		}

		#endregion


		#region Methods: Overrideable empty implementation of IJQueryControl

		/// <summary>
		/// Renders the load script required to generate the client-side jQuery widget.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="script">A GreenIsomer.Web.Controls.JQuery.ScriptBuilder that creates the output stream to render jQuery JavaScript on the client.</param>
		protected virtual void RenderLoadScript(ScriptBuilder script) { }

		/// <summary>
		/// Renders the jQuery "Ready Event" script required to generate the client-side jQuery widget.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="script">A GreenIsomer.Web.Controls.JQuery.ScriptBuilder that creates the output stream to render jQuery JavaScript on the client.</param>
		protected virtual void RenderReadyScript(ScriptBuilder script) { }

		#endregion


		#region Methods: Rendering

		/// <summary>
		/// Raises the System.Web.UI.Control.Load event.
		/// </summary>
		/// <param name="e">The System.EventArgs object that contains the event data.</param>
		protected override void OnLoad(System.EventArgs e) {
			base.OnLoad(e);

			Register(Manager.GetCurrent(Page));
		}

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The GreenIsomer.Web.Controls.JQuery.Manager used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected virtual void Register(Manager manager) { }

		/// <summary>
		/// Creates the properties object that will be rendered as the jQuery UI initialization options.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>A GreenIsomer.Web.Controls.JQuery.Properties object that will become the jQuery UI initialization options.</returns>
		protected virtual PropertyCollection CreateProperties() {
			return new PropertyCollection();
		}

		#endregion


		#region Explicit implementation: IJQueryControl

		void IJQueryControl.RenderLoadScript(ScriptBuilder script) {
			RenderLoadScript(script);
		}

		void IJQueryControl.RenderReadyScript(ScriptBuilder script) {
			RenderReadyScript(script);
		}

		#endregion


	}

}